package com.example.demo.controller;

import com.example.demo.model.Student;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@RequestMapping("/calc")
@RestController
public class RequestController{

    @RequestMapping("/sum")
    public String sum(Integer num1,Integer num2){
        Integer sum =num1+num2;
        return "计算机结果："+sum;
    }
    /*
    可以修饰类，也可以修饰方法
    1.修饰类的时候，表示这个类下的所有方法，返回的均为数据
    2.修饰方法时，表示该方法返回的是数据
    * */
    @ResponseBody
    @RequestMapping("/r1")
    public String r1(){
        return "标识类方法时，hello Spring MVC";
    }

    @RequestMapping("/r2")
    public String r2(String [] name){
        return Arrays.toString(name);
    }

    @RequestMapping( "/r3")
    public String r3(@RequestParam Student student){

        return "接收参数："+student;
    }

    @RequestMapping("/r4")
    public String r4(Integer id ,String name){
        return "接收参数 id："+id+"  name: "+name;
    }

    @RequestMapping("/r5")
    public String r4(@RequestParam(value = "name",required = false) String usename){
        return "接收参数 id："+"  name: "+usename;
    }

    @RequestMapping("/r6")
    public String r6(@RequestParam List<String> list){
        return "list长度："+list.size()+"  接收参数 list:："+list;
    }
    /*
    * 从路径中获取id
    * */
    @RequestMapping("/r7/{r7}")
    public String r7(@PathVariable("r7") Integer ret){
        return "从路径中获取id："+ret;
    }
    /*
     * 上传文件
     * @RequestParam("File")重命名
     * */
    @RequestMapping("/r8")
    public String r8(@RequestPart("File") MultipartFile jpgFile){
        String originalFilename = jpgFile.getOriginalFilename();
        return "接收到文件名称：："+originalFilename;
    }
    @RequestMapping("/r9")
    public String r9(String name){
        return "接收参数:"+name;
    }
    //RequestBody: 请求正⽂，意思是这个注解作⽤在请求正⽂的数据绑定，请求参数必须在写在请求正⽂中
    @RequestMapping("/r10")
    public String r9(@RequestBody Student student){
        return "接收参数:"+student.toString();
    }
    /*
    * 从url中获取数据
    * */
    @RequestMapping("/r11/{name}/{id}")
    public String method(@PathVariable("name") String name,@PathVariable Integer id){
        return "接收参数:"+name+" "+id;
    }

    /*
    * 返回JSON
    * */
    @RequestMapping("/returnJson")
    @ResponseBody
    public HashMap<String,String> returnJosn(){
        HashMap<String, String> map = new HashMap<>();
        map.put("Java", "Java Value");
        map.put("MySQL", "MySQL Value");
        map.put("Redis", "Redis Value");
        return map;
    }
    /*
     * 设置状态码
     * */
    @RequestMapping(value = "/setStatus")
    @ResponseBody
    public String setStatus(HttpServletResponse response){
       response.setStatus(401);
       return "设置状态码成功 ";
    }

    @RequestMapping(value = "/returnJson2",produces = "application/json")
    @ResponseBody
    public String returnJson2() {
        return "{\"success\":true}";
    }

}
